﻿using System.Linq;

namespace System.Reflection.Extension
{
    public static class PropertyInfoExtension
    {
        public static void ForEachAttribute(this PropertyInfo property, Action<Attribute> callBack)
        {
            object[] attributes = property.GetCustomAttributes(true);
            foreach (Attribute attribute in attributes)
                callBack(attribute);
        }
        public static bool HasAttribute<T>(this PropertyInfo property) where T : Attribute
        {
            bool result = false;
            property.ForEachAttribute(a => result = (result || a.GetType().Equals(typeof(T))));
            return result;
        }
        public static bool isPrimitive(this PropertyInfo field)
        {
            return field.PropertyType.IsPrimitive || field.PropertyType.Equals(typeof(string));
        }

        public static bool isInclusive(this PropertyInfo field, params Type[] inclusives)
        {
            return inclusives.Contains(field.PropertyType);
        }

        public static bool isClass(this PropertyInfo field)
        {
            return field.PropertyType.IsClass && field.PropertyType.IsAnsiClass &&
                !field.PropertyType.IsGenericType && field.PropertyType != typeof(string);
        }

        public static bool IsValueType(this PropertyInfo field)
        {
            return field.PropertyType.IsValueType;
        }

        public static bool isGeneric(this PropertyInfo field)
        {
            return field.PropertyType.IsGenericType;
        }
    }
}